*******************************************************************************
*``The Costs and Environmental Justice Concerns of NIMBY in Solid Waste Disposal''*
* Phuong Ho (SNF-Centre for Applied Research in Norwegian School of Economics) *
*******************************************************************************
************** This file makes Table 4  ************************
cls
clear all 
capture log close
set more off

global mydir "C:\XYZ" // set working directory
global data "$mydir\data"
global matlabin "$mydir\matlab\input"
global result "$mydir\TEX\"
cd "$mydir\stata"
set matsize 11000
set maxvar 32700

************ MAKE TABLE OF COUNTERFACTUALS *********************************
******************* TABLE 4 ***************************************
****************************************************************************
***0. SELECT THE COUNTERFACTUAL *******************************************
local scenario "importBan importTax" 

**************************************************************************
**** BUILD THE SAMPLE. IMPORT STATUS QUO ESTIMATES **************************
***************************************************************************
import excel "$mydir\matlab\output\nimby_statusquo.xlsx", sheet("Sheet1") firstrow clear
label var sj_q "market size"
label var pr "model implied market share"
label var Bq "model implied trash amount"
label var el "price elasticity"
label var CS "hauler's cost"
sort TCJ
save nimby_counterfactual, replace

**************************************************************************
****** IMPORT DEMOGRAPHICS IN 3-MILE COMMUNITIES *************************
***************************************************************************
*****************************************************************************
// 1/3. Import demographics for county of origin 
use $data\demographics\raw\county_demographics_2010_CA, clear
keep *geoid *name *pop *white* *black* *hispanic* *asian* *mdhhinc
foreach x of varlist _all {
	rename `x' O_`x'
} 
rename O_COname county
gen year=2010
sort year county
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year county
merge m:1 year county using `tmp'
erase `tmp'
drop if _merge==2 //_merge==1 are not 2010, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace O_COmdhhinc=O_COmdhhinc*index2000
save nimby_counterfactual, replace

// 2/3. Import demographics for county of destination 
use $data\demographics\raw\county_demographics_2010_CA, clear
keep *geoid *name *pop *white* *black* *hispanic* *asian* *mdhhinc
foreach x of varlist _all {
	rename `x' D_`x'
} 
rename D_COname descnty
gen year=2010
sort year descnty
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year descnty
merge m:1 year descnty using `tmp'
erase `tmp'
*list facility descnty if _merge==1 //must be oos
*list  year county facility descnty if _merge==2 
drop if _merge==2 //_merge==1 are not 2010, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace D_COmdhhinc=D_COmdhhinc*index2000
save nimby_counterfactual, replace

// 3/3. Import demographics for community of destination
use $data\buffer\demographics_buffer_3mile, clear
keep code year Bpop B0pop Bwhite Bblack Basian Bhispanic Bmdhhinc
local varl "white black asian hispanic"
foreach v in `varl' {
  gen B`v'_p=B`v'*100/Bpop
}
foreach x of varlist _all {
	rename `x' D_`x'
} 
rename D_code Code
rename D_year year
sort year Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year Code
merge m:1 year Code using `tmp'
erase `tmp'
*list facility descnty if _merge==1 //must be oos
*list  year county facility descnty if _merge==2 // San Francisco, Alpine, Nevada (don't have any landfills) Yuba and Sutter since both merge
drop if _merge==2 //_merge==1 must be 0 if analyzing at other years, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace D_Bmdhhinc=D_Bmdhhinc*index2000 if year==2010  // adjust income to 2000 dollars
save nimby_counterfactual, replace
****************************************************************************
**** NOW, DIFFERENT SCENARIO DOES DIFFERENT CODE ****************************

use nimby_counterfactual, clear
bys year Code: gen dupj=cond(_N==1,0,_n)
bys year county: gen dupc=cond(_N==1,0,_n)
bys year descnty: gen dupd=cond(_N==1,0,_n)
bys year quarter county: gen duptc=cond(_N==1,0,_n)
save nimby_counterfactual, replace
use nimby_counterfactual, clear 

foreach s in `scenario' {
	if "`s'"=="importBan" {
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********
local s "importBan"
import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS Crealprice Cpmc Cmarkup
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
replace TC=99999 if localf==0
sort year county
by year county: egen slocal=total(localf)
replace slocal=1 if slocal>0 & slocal<. // whether a county has a local facility
rename slocal havef
bys yq county: egen slocal=total(localf)
replace slocal=1 if slocal>0 & slocal<.
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
**Adjust and rename realprice, dridis, diesel price depending on counterfactual************
bys yq Code: egen Crealprice_del=mean(Crealprice)
drop Crealprice
rename Crealprice_del Crealprice
gen Nrealprice=Crealprice
gen Ndridis=dridis

bys yq Code: egen Nmarkup=mean(Cmarkup)
*mdesc markup Nmarkup if year==2010
*list yq county Code desco Crealprice realprice markup Cmarkup if year==2010 & Cmarkup==.

*spread values because I drop some observations when doing counterfactuals
bys year quarter county: egen CCS_del=mean(CCS)
drop CCS
rename CCS_del CCS
gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

******************ECONOMIC VARIABLES *********************
*** Calculate policy variables at current state (with actual q)
keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
******************** CURRENT STATISTICS. ACTUAL LEVEL *****************
**calculate export amount, import amount, and net import by county
sort year county  // exports
by year county: egen Atcq=total(q)
*export to other county, but within California
by year county: egen Aexpq_del= total(q) if localf==0 & j~="oos" 
by year county: egen Aexpq= mean(Aexpq_del)
replace Aexpq=0 if Aexpq==.
sort year desco // imports
*import by destination county
by year desco: egen Aimpdes_del =total(q) if localf==0 & j~="oos" 
by year desco: egen Aimpdes=mean(Aimpdes_del) // spread values to des cnty level
replace Aimpdes=0 if Aimpdes==.
gen Aimpq_del= Aimpdes if county==desco // spread values from des cnty to county
sort year county
by year county: egen Aimpq=mean(Aimpq_del) // spread values to county
replace Aimpq=0 if Aimpq==.
drop *_del Aimpdes
gen Anetim= Aimpq - Aexpq // net import by county

**total receiving trash, by county
sort year desco
by year desco: egen Arqdes_del=total(q)
gen Arq_del=Arqdes_del if county==desco // spread values from descnty to county
sort year county
by year county: egen Arq=mean(Arq_del)
drop *_del

**payment, total tipping fees
sort year county // exports
gen Adol_del=q*realprice
by year county: egen Acdol=total(Adol_del) // total tipping fees
replace Acdol=0 if Acdol==.
by year county: egen Aexdol_c_del=total(Adol_del) if localf==0 & j~="oos"
by year county: egen Aexdol=mean(Aexdol_c_del)
replace Aexdol=0 if Aexdol==.
sort year desco // imports
by year desco: egen Aimdol_d_del=total(Adol_del) if localf==0 & j~="oos"
by year desco: egen Aimdold=mean(Aimdol_d_del)
replace Aimdold=0 if Aimdold==.
gen Aimdol_c_del=Aimdold if county==desco // spread values from des cnty to county
sort year county
by year county: egen Aimdol=mean(Aimdol_c_del)
replace Aimdol=0 if Aimdol==.
gen Anetimdol=Aimdol-Aexdol
drop *_del
gen Acdolpt=Acdol/Atcq // dollar per ton

**trash miles
gen Acmil_del=q*dridis
sort year county
by year county: egen Acmil=total(Acmil_del) // total trash ton-miles
replace Acmil=0 if Acmil==.
drop *_del
gen Acmilpt=Acmil/Atcq // mile per ton

******************* SIMULATED/MODEL IMPLIED STATISTICS ****************
**calculate export amounts and import amount using q_tcj
sort year county  // exports
by year county: egen tcq=total(Bq)
*export to other county, but within California
by year county: egen expq_del= total(Bq) if localf==0 & j~="oos"
by year county: egen expq= mean(expq_del)
replace expq=0 if expq==.
sort year desco //imports
*import by destination county
by year desco: egen impdes_del =total(Bq) if localf==0 & j~="oos" 
by year desco: egen impdes=mean(impdes_del) // spread values to des cnty level
replace impdes=0 if impdes==.
gen impq_del= impdes if county==desco // spread values from des cnty to county
sort year county
by year county: egen impq=mean(impq_del) // spread values to county
replace impq=0 if impq==.
drop *_del impdes
gen netim= impq - expq // net import by county

**total receiving trash, by county
sort year desco
by year desco: egen rqdes_del=total(Bq)
gen rq_del=rqdes_del if county==desco // spread values from descnty to county
sort year county
by year county: egen rq=mean(rq_del)
drop *_del

**payment, total tipping fees
sort year county //exports
gen dol_del=Bq*realprice
by year county: egen cdol=total(dol_del) // total tipping fees
replace cdol=0 if cdol==.
by year county: egen exdol_c_del=total(dol_del) if localf==0 & j~="oos"
by year county: egen exdol=mean(exdol_c_del)
replace exdol=0 if exdol==.
sort year desco //imports
by year desco: egen imdol_d_del=total(dol_del) if localf==0 & j~="oos"
by year desco: egen imdold=mean(imdol_d_del)
replace imdold=0 if imdold==.
gen imdol_c_del=imdold if county==desco //spread values from des cnty to county
sort year county
by year county: egen imdol=mean(imdol_c_del)
replace imdol=0 if imdol==.
gen netimdol=imdol-exdol
drop*_del
gen cdolpt=cdol/tcq // dollar per ton
gen exdolpt=0

*trash miles
gen cmil_del=Bq*dridis
sort year county
by year county: egen cmil=total(cmil_del) // total trash ton-miles
replace cmil=0 if cmil==.  // trash mileage
by year county: egen exmil_c_del=total(cmil_del) if localf==0 & j~="oos"
by year county: egen exmil=mean(exmil_c_del)
replace exmil=0 if exmil==.
drop *_del
gen cmilpt=cmil/tcq // mile per trip
gen exmilpt=exmil/expq // mile per trip for exports

********************NEW LEVELS *******
**calculate export amounts and import amount using q_tcj
sort year county  // exports
by year county: egen Ntcq_del=total(Cq) if localf==1
by year county: egen Ntcq=mean(Ntcq_del)
gen Nexpq_del= 0
by year county: egen Nexpq= mean(Nexpq_del)
replace Nexpq=0 if Nexpq==.
sort year desco //imports
gen Nimpdes_del =0 // calculate import by des county
by year desco: egen Nimpdes=mean(Nimpdes_del) // spread values to des cnty level
replace Nimpdes=0 if Nimpdes==.
gen Nimpq_del= Nimpdes if county==desco // spread values from des cnty to county
sort year county
by year county: egen Nimpq=mean(Nimpq_del) // spread values to county
replace Nimpq=0 if Nimpq==.
drop *_del Nimpdes
gen Nnetim= Nimpq - Nexpq

**total receiving trash, by county
sort year desco
by year desco: egen Nrqdes_del=total(Cq)
gen Nrq_del=Nrqdes_del if county==desco // spread values from descnty to county
sort year county
by year county: egen Nrq=mean(Nrq_del)
drop *_del

**payment, total tipping fees
sort year county //exports
gen Ndol_del=Cq*Nrealprice
*the ban makes sure all trash go to local facility, preventing rounding errors
by year county: egen Ncdol_del=total(Ndol_del) if localf==1
by year county: egen Ncdol=mean(Ncdol_del) 
replace Ncdol=0 if Ncdol==.
gen Nexdol_c_del=0
by year county: egen Nexdol=mean(Nexdol_c_del)
replace Nexdol=0 if Nexdol==.
sort year desco //imports
gen Nimdol_d_del=0
by year desco: egen Nimdold=mean(Nimdol_d_del)
replace Nimdold=0 if Nimdold==.
gen Nimdol_c_del=Nimdold if county==desco //spread values from des cnty to county
sort year county
by year county: egen Nimdol=mean(Nimdol_c_del)
replace Nimdol=0 if Nimdol==.
gen Nnetimdol=Nimdol-Nexdol
drop*_del
gen Ncdolpt=Ncdol/Ntcq // dollar per ton
gen Nexdolpt=0

*trash miles
gen Ncmil_del=Cq*Ndridis
sort year county
by year county: egen Ncmil_2_del=total(Ncmil_del) if localf==1
by year county: egen Ncmil=mean(Ncmil_2_del)
replace Ncmil=0 if Ncmil==.
gen Ncmilpt=Ncmil/Ntcq // mile per trip
by year county: egen Nexmil_c_del=total(Ncmil_del) if localf==0 & j~="oos"
by year county: egen Nexmil=mean(Nexmil_c_del)
replace Nexmil=0 if Nexmil==.
drop *_del
gen Nexmilpt=Nexmil/Nexpq // mile per trip for exports

**hauler cost
sort year quarter county
by year quarter county: egen cntybq=total(Bq)
by year quarter county: egen cntycq=total(Cq)
gen totCS=CS*cntybq
gen NtotCS=CCS*cntycq
*gen ADtotCS=NtotCS-totCS
cap gen nonlocal=1-localf
sort year county quarter nonlocal
by year county quarter: gen dup=cond(_N==1,0,_n)
by year county: egen ctotCS_del=total(totCS) if dup<=1
by year county: egen ctotCS=mean(ctotCS_del)
by year county: egen NctotCS_del=total(NtotCS) if localf==1 & dup<=1
by year county: egen NctotCS=mean(NctotCS_del) 
gen ctotCSpt=ctotCS/tcq // dollar per ton
gen NctotCSpt=NctotCS/Ntcq // dollar per ton
drop *_del
gen expqp = expq*100/tcq // percentage of export

gen impqp = impq*100/tcq
******** change in external cost of transportation ******
*Austin2018 estimates external cost of truck trans=2.62-5.86 in 2014 cents per ton-mile
*This means 1.91-4.26 in 2000 cents per ton-mile
gen extran1=cmil*1.91/100 // in dollar
gen extran2=cmil*4.26/100
gen Nextran1=Ncmil*1.91/100 // in dollar
gen Nextran2=Ncmil*4.26/100
*ADextran <0: reduce external trans cost

******* change in price at facility level ***************
egen tagfq=tag(year quarter Code)
bys year Code: egen lanpri_del=mean(realprice) if tagfq==1
bys year Code: egen lanpri=mean(lanpri_del)
bys year Code: egen Clanpri_del=mean(Crealprice) if tagfq==1
bys year Code: egen Clanpri=mean(Clanpri_del)
drop *_del

egen tagf=tag(year Code) 

by year: egen avgprice_del=mean(lanpri) if tagf==1
by year: egen avgprice=mean(avgprice_del)
by year: egen Navgprice_del=mean(Clanpri) if tagf==1
by year: egen Navgprice=mean(Navgprice_del)
by year: egen atcq=mean(tcq) if dupc<=1 & havelocalc==1 
by year: egen wtcq=total(tcq*tcq) if dupc<=1 & havelocalc==1 
by year: egen tcq_del=total(tcq) if dupc<=1 & havelocalc==1 
replace wtcq=wtcq/tcq_del
drop *_del

*There may be difference change bw facility that received foreign flow vs local
*tag facility that received intercounty flow before policy.
egen tagff=tag(year Code) if localf==0 & q>0
bys year Code: egen havefq=total(tagff)
by year: egen avgpricef_del=mean(lanpri) if havefq==1 & tagf==1
by year: egen avgpricef=mean(avgpricef_del)
by year: egen Navgpricef_del=mean(Clanpri) if havefq==1 & tagf==1
by year: egen Navgpricef=mean(Navgpricef_del)
by year: egen avgpricel_del=mean(lanpri) if havefq==0 & tagf==1
by year: egen avgpricel=mean(avgpricel_del)
by year: egen Navgpricel_del=mean(Clanpri) if havefq==0 & tagf==1
by year: egen Navgpricel=mean(Navgpricel_del)
drop *_del

bys year Code: egen fqf_del=total(Bq) if havefq==1
bys year Code: egen fqf=mean(fqf_del)
bys year Code: egen Nfqf_del=total(Cq) if havefq==1
bys year Code: egen Nfqf=mean(Nfqf_del)
bys year Code: egen fql_del=total(Bq) if havefq==0
bys year Code: egen fql=mean(fql_del)
bys year Code: egen Nfql_del=total(Cq) if havefq==0
bys year Code: egen Nfql=mean(Nfql_del)

bys year Code: egen fqff_del=total(Bq) if havefq==1 & localf==0 // intercnty waste at intercnty sites
bys year Code: egen fqff=mean(fqff_del)
bys year Code: egen Nfqff_del=total(Cq) if havefq==1 & localf==0
bys year Code: egen Nfqff=mean(Nfqff_del)

bys year Code: egen fqlf_del=total(Bq) if havefq==0 & localf==0 // intercnty waste at intracnty sites
bys year Code: egen fqlf=mean(fqlf_del)
bys year Code: egen Nfqlf_del=total(Cq) if havefq==0 & localf==0
bys year Code: egen Nfqlf=mean(Nfqlf_del)
bys year Code: egen Afqlf_del=total(q) if havefq==0 & localf==0
bys year Code: egen Afqlf=mean(Afqlf_del)

drop *_del
local varl "fqf fql fqff fqlf"
foreach v in `varl' {
	gen AD`v'=N`v' - `v'
	*gen AD`v'p=AD`v'*100/`v'
}
replace ADfqlf=0 if Afqlf==0 & ADfqlf<0
replace ADfqlf=0 if havefq==0 & ADfqlf==.

**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "tcq rq interq expq impq cdol cmil ctotCS extran1 extran2 avgprice avgpricef avgpricel"
foreach v in `varl' {
  gen AD`v'=N`v' -`v'
  gen AD`v'p=AD`v'*100/`v'
}
local varl "cdol cmil ctotCS"
foreach v in `varl' {
gen AD`v'pt=AD`v'/atcq
}

**change by county ******
local varl "cdol cmil ctotCS"
foreach v in `varl' {
gen AD`v'ptc=AD`v'/tcq
}

gen ADextran=ADcmilptc*wtcq*1.91/100

***benefits of reduction in export to break even with the hauling cost ***
*each ycounty has importq impq and cost ctotCS
by year: egen aADctotCS=total(ADctotCS*tcq) if year==2010  & dupc<=1 & havelocalc==1 
by year: egen aADimpq=total(ADimpq*tcq) if year==2010  & dupc<=1 & havelocalc==1 
gen hhvalue=-aADctotCS/aADimpq // $/ton of import for places reducing import
gen hhvaluec=-ADctotCS/ADimpq
*******###############################################################********
*******************************************************************************
di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"
label var interq "CA intercounty waste (baseline)"
label var ADinterq "change in intercounty waste in CA"
label var expqp "percentage of export (baseline)"
label var expq "export amount (baseline)"
label var ADexpq "change in export amount"
label var impqp "percentage of import (baseline)"
label var impq "import amount (baseline)"
label var ADimpq "change in import amount"
label var cdolpt "tipping fees (baseline)"
label var ADcdolptc "change in tipping fees"
label var cmilpt "mileage (baseline)"
label var ADcmilptc "change in mileage"
label var ADctotCSptc "change in discarding cost"
label var extran1 "external transport cost (baseline)"
label var ADextran "change in external transport cost"
label var hhvaluec "household value"
label var fqf "waste at intercounty sites"
label var ADfqf "change in waste at intercounty sites"
label var fql "waste at intracounty sites"
label var ADfql "change in waste at intracounty sites"
label var fqff "intercounty waste at intercounty sites"
label var ADfqff "change in intercounty waste at intercounty sites"
label var ADfqlf "change in intercounty waste at intracounty sites"
label var avgprice "average price at a facility (baseline)"
label var ADavgprice "change in average price at a facility"
label var ADavgpricep "change in average price at a facility (%)"
label var avgpricef "average price at non-local facilities (baseline)"
label var ADavgpricef "change in average price at non-local facilities"
label var ADavgpricefp "change in average price at non-local facilities (%)"
label var avgpricel "average price at local facilities (baseline)"
label var ADavgpricel "change in average price at local facilities"
label var ADavgpricelp "change in average price at local facilities (%)"

sum interq ADinterq expqp expq ADexpq impqp impq ADimpq cdolpt ADcdolptc ///
	cmilpt ADcmilptc ADctotCSptc extran1 ADextran hhvaluec ///
	[iw=tcq] if year==2010  & dupc<=1 & havelocalc==1 

sum avgprice ADavgprice ADavgpricep ///
	avgpricef ADavgpricef ADavgpricefp ///
	avgpricel ADavgpricel ADavgpricelp ///
	fqf ADfqf fql ADfql fqff ADfqff ADfqlf if year==2010 & tagf==1

} // ending "`s'"=="importBan"

******************************************************************************
************ NEW COUNTERFACTUAL. IMPORT TAX ********************************
******************************************************************************
if "`s'"!="importBan" {
**** Calculate baseline level *********
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********
import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS Crealprice Cpmc Cmarkup
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
**Adjust and rename realprice, dridis, diesel price depending on counterfactual************
gen Nrealprice=Crealprice
if "`s'"=="importTax" {
replace Nrealprice=Nrealprice*1.10  if localf==0
}
if "`s'"=="trashTax" {
replace Nrealprice=Nrealprice*1.10
}

gen Ndridis=dridis
*spread values because I drop some observations when doing counterfactuals
bys year quarter county: egen CCS_del=mean(CCS)
drop CCS
rename CCS_del CCS
*********************************CORRECT VARIABLES *************************
*use trash_distn_counterfactual, clear
*Correct rounding errors for the ban. After the ban, trash must be dumped locally 
gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

******************ECONOMIC VARIABLES *********************
*** Calculate policy variables at current state (with actual q)
keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
******************** CURRENT STATISTICS. ACTUAL LEVEL *****************
**calculate export amount, import amount, and net import by county
sort year county  // exports
by year county: egen Atcq=total(q)
*export to other county, but within California
by year county: egen Aexpq_del= total(q) if localf==0 & j~="oos" 
by year county: egen Aexpq= mean(Aexpq_del)
replace Aexpq=0 if Aexpq==.
sort year desco // imports
*import by destination county
by year desco: egen Aimpdes_del =total(q) if localf==0 & j~="oos" 
by year desco: egen Aimpdes=mean(Aimpdes_del) // spread values to des cnty level
replace Aimpdes=0 if Aimpdes==.
gen Aimpq_del= Aimpdes if county==desco // spread values from des cnty to county
sort year county
by year county: egen Aimpq=mean(Aimpq_del) // spread values to county
replace Aimpq=0 if Aimpq==.
drop *_del Aimpdes
gen Anetim= Aimpq - Aexpq // net import by county

**total receiving trash, by county
sort year desco
by year desco: egen Arqdes_del=total(q)
gen Arq_del=Arqdes_del if county==desco // spread values from descnty to county
sort year county
by year county: egen Arq=mean(Arq_del)
drop *_del

**payment, total tipping fees
sort year county // exports
gen Adol_del=q*realprice
by year county: egen Acdol=total(Adol_del) // total tipping fees
replace Acdol=0 if Acdol==.
by year county: egen Aexdol_c_del=total(Adol_del) if localf==0 & j~="oos"
by year county: egen Aexdol=mean(Aexdol_c_del)
replace Aexdol=0 if Aexdol==.
sort year desco // imports
by year desco: egen Aimdol_d_del=total(Adol_del) if localf==0 & j~="oos"
by year desco: egen Aimdold=mean(Aimdol_d_del)
replace Aimdold=0 if Aimdold==.
gen Aimdol_c_del=Aimdold if county==desco // spread values from des cnty to county
sort year county
by year county: egen Aimdol=mean(Aimdol_c_del)
replace Aimdol=0 if Aimdol==.
gen Anetimdol=Aimdol-Aexdol
drop *_del
gen Acdolpt=Acdol/Atcq // dollar per ton

**trash miles
gen Acmil_del=q*dridis
sort year county
by year county: egen Acmil=total(Acmil_del) // total trash ton-miles
replace Acmil=0 if Acmil==.
drop *_del
gen Acmilpt=Acmil/Atcq // dollar per ton

******************* SIMULATED/MODEL IMPLIED STATISTICS ****************
**calculate export amounts and import amount using q_tcj
sort year county  // exports
by year county: egen tcq=total(Bq)
*export to other county, but within California
by year county: egen expq_del= total(Bq) if localf==0 & j~="oos"
by year county: egen expq= mean(expq_del)
replace expq=0 if expq==.
sort year desco //imports
*import by destination county
by year desco: egen impdes_del =total(Bq) if localf==0 & j~="oos" 
by year desco: egen impdes=mean(impdes_del) // spread values to des cnty level
replace impdes=0 if impdes==.
gen impq_del= impdes if county==desco // spread values from des cnty to county
sort year county
by year county: egen impq=mean(impq_del) // spread values to county
replace impq=0 if impq==.
drop *_del impdes
gen netim= impq - expq // net import by county

**total receiving trash, by county
sort year desco
by year desco: egen rqdes_del=total(Bq)
gen rq_del=rqdes_del if county==desco // spread values from descnty to county
sort year county
by year county: egen rq=mean(rq_del)
drop *_del

**payment, total tipping fees
sort year county //exports
gen dol_del=Bq*realprice
by year county: egen cdol=total(dol_del) // total tipping fees
replace cdol=0 if cdol==.
by year county: egen exdol_c_del=total(dol_del) if localf==0 & j~="oos"
by year county: egen exdol=mean(exdol_c_del)
replace exdol=0 if exdol==.
sort year desco //imports
by year desco: egen imdol_d_del=total(dol_del) if localf==0 & j~="oos"
by year desco: egen imdold=mean(imdol_d_del)
replace imdold=0 if imdold==.
gen imdol_c_del=imdold if county==desco //spread values from des cnty to county
sort year county
by year county: egen imdol=mean(imdol_c_del)
replace imdol=0 if imdol==.
gen netimdol=imdol-exdol
gen cdolpt=cdol/tcq // dollar per ton
drop *_del
gen exdolpt=exdol/expq // dollar per ton for exports

*trash miles
gen cmil_del=Bq*dridis
sort year county
by year county: egen cmil=total(cmil_del) // total trash ton-miles
replace cmil=0 if cmil==.  // trash mileage
gen cmilpt=cmil/tcq // dollar per ton

by year county: egen exmil_c_del=total(cmil_del) if localf==0 & j~="oos"
by year county: egen exmil=mean(exmil_c_del)
replace exmil=0 if exmil==.
drop *_del
gen exmilpt=exmil/expq // mile per trip for exports

********************NEW LEVELS *******
**calculate export amounts and import amount using q_tcj
sort year county  // exports
by year county: egen Ntcq_del=total(Cq)
by year county: egen Ntcq=mean(Ntcq_del)
by year county: egen Nexpq_del= total(Cq) if localf==0 & j~="oos"
by year county: egen Nexpq= mean(Nexpq_del)
replace Nexpq=0 if Nexpq==.
sort year desco //imports
by year desco: egen Nimpdes_del =total(Cq) if localf==0 & j~="oos" // calculate import by des county
by year desco: egen Nimpdes=mean(Nimpdes_del) // spread values to des cnty level
replace Nimpdes=0 if Nimpdes==.
gen Nimpq_del= Nimpdes if county==desco // spread values from des cnty to county
sort year county
by year county: egen Nimpq=mean(Nimpq_del) // spread values to county
replace Nimpq=0 if Nimpq==.
drop *_del Nimpdes
gen Nnetim= Nimpq - Nexpq

**total receiving trash
sort year desco
by year desco: egen Nrqdes_del=total(Cq)
gen Nrq_del=Nrqdes_del if county==desco // spread values from descnty to county
sort year county
by year county: egen Nrq=mean(Nrq_del)
drop *_del

*payment
sort year county //exports
gen Ndol_del=Cq*Nrealprice
by year county: egen Ncdol=total(Ndol_del)
replace Ncdol=0 if Ncdol==.
by year county: egen Nexdol_c_del=total(Ndol_del) if localf==0 & j~="oos"
by year county: egen Nexdol=mean(Nexdol_c_del)
replace Nexdol=0 if Nexdol==.
sort year desco //imports
by year desco: egen Nimdol_d_del=total(Ndol_del) if localf==0 & j~="oos"
by year desco: egen Nimdold=mean(Nimdol_d_del)
replace Nimdold=0 if Nimdold==.
gen Nimdol_c_del=Nimdold if county==desco //spread values from des cnty to county
sort year county
by year county: egen Nimdol=mean(Nimdol_c_del)
replace Nimdol=0 if Nimdol==.
gen Nnetimdol=Nimdol-Nexdol
gen Ncdolpt=Ncdol/Ntcq // dollar per ton
drop *_del
gen Nexdolpt=Nexdol/Nexpq // dollar per ton for exports

*trash miles
gen Ncmil_del=Cq*Ndridis
sort year county
by year county: egen Ncmil=total(Ncmil_del)
replace Ncmil=0 if Ncmil==.
gen Ncmilpt=Ncmil/Ntcq // mile per trip
by year county: egen Nexmil_c_del=total(Ncmil_del) if localf==0 & j~="oos"
by year county: egen Nexmil=mean(Nexmil_c_del)
replace Nexmil=0 if Nexmil==.
drop *_del
gen Nexmilpt=Nexmil/Nexpq // mile per trip for exports

sort year quarter county
by year quarter county: egen cntybq=total(Bq)
by year quarter county: egen cntycq=total(Cq)
gen totCS=CS*cntybq
gen NtotCS=CCS*cntycq
*gen ADtotCS=NtotCS-totCS
sort year county quarter
by year county quarter: gen dup=cond(_N==1,0,_n)
by year county: egen ctotCS_del=total(totCS) if dup<=1
by year county: egen ctotCS=mean(ctotCS_del)
by year county: egen NctotCS_del=total(NtotCS) if  dup<=1
by year county: egen NctotCS=mean(NctotCS_del) 
gen ctotCSpt=ctotCS/tcq // dollar per ton
gen NctotCSpt=NctotCS/Ntcq // dollar per ton
drop *_del
gen expqp = expq*100/tcq // percentage of export
*gen Nexpqp= Nexpqp*100/tcq
gen impqp = impq*100/tcq
******** change in external cost of transportation ******
*Austin2018 estimates external cost of truck trans=2.62-5.86 in 2014 cents per ton-mile
*This means 1.91-4.26 in 2000 cents per ton-mile
gen extran1=cmil*1.91/100 // in dollar
gen extran2=cmil*4.26/100
gen Nextran1=Ncmil*1.91/100 // in dollar
gen Nextran2=Ncmil*4.26/100
*ADextran <0: reduce external trans cost
	
******* change in price at facility level ***************
egen tagfq=tag(year quarter Code)
bys year Code: egen lanpri_del=mean(realprice) if tagfq==1
bys year Code: egen lanpri=mean(lanpri_del)
bys year Code: egen Clanpri_del=mean(Crealprice) if tagfq==1
bys year Code: egen Clanpri=mean(Clanpri_del)
drop *_del

egen tagf=tag(year Code) 

by year: egen avgprice_del=mean(lanpri) if tagf==1
by year: egen avgprice=mean(avgprice_del)
by year: egen Navgprice_del=mean(Clanpri) if tagf==1
by year: egen Navgprice=mean(Navgprice_del)
by year: egen atcq=mean(tcq) if dupc<=1 & havelocalc==1 
by year: egen wtcq=total(tcq*tcq) if dupc<=1 & havelocalc==1 
by year: egen tcq_del=total(tcq) if dupc<=1 & havelocalc==1 
replace wtcq=wtcq/tcq_del
drop *_del

*There may be difference change bw facility that received foreign flow vs local
*tag facility that received intercounty flow before policy.
egen tagff=tag(year Code) if localf==0 & q>0
bys year Code: egen havefq=total(tagff)
by year: egen avgpricef_del=mean(lanpri) if havefq==1 & tagf==1
by year: egen avgpricef=mean(avgpricef_del)
by year: egen Navgpricef_del=mean(Clanpri) if havefq==1 & tagf==1
by year: egen Navgpricef=mean(Navgpricef_del)
by year: egen avgpricel_del=mean(lanpri) if havefq==0 & tagf==1
by year: egen avgpricel=mean(avgpricel_del)
by year: egen Navgpricel_del=mean(Clanpri) if havefq==0 & tagf==1
by year: egen Navgpricel=mean(Navgpricel_del)
drop *_del

bys year Code: egen fqf_del=total(Bq) if havefq==1
bys year Code: egen fqf=mean(fqf_del)
bys year Code: egen Nfqf_del=total(Cq) if havefq==1
bys year Code: egen Nfqf=mean(Nfqf_del)
bys year Code: egen fql_del=total(Bq) if havefq==0
bys year Code: egen fql=mean(fql_del)
bys year Code: egen Nfql_del=total(Cq) if havefq==0
bys year Code: egen Nfql=mean(Nfql_del)

bys year Code: egen fqff_del=total(Bq) if havefq==1 & localf==0 // intercnty waste at intercnty sites
bys year Code: egen fqff=mean(fqff_del)
bys year Code: egen Nfqff_del=total(Cq) if havefq==1 & localf==0
bys year Code: egen Nfqff=mean(Nfqff_del)

bys year Code: egen fqlf_del=total(Bq) if havefq==0 & localf==0 // intercnty waste at intracnty sites
bys year Code: egen fqlf=mean(fqlf_del)
bys year Code: egen Nfqlf_del=total(Cq) if havefq==0 & localf==0
bys year Code: egen Nfqlf=mean(Nfqlf_del)
bys year Code: egen Afqlf_del=total(q) if havefq==0 & localf==0
bys year Code: egen Afqlf=mean(Afqlf_del)

drop *_del
local varl "fqf fql fqff fqlf"
foreach v in `varl' {
	gen AD`v'=N`v' - `v'
	*gen AD`v'p=AD`v'*100/`v'
}
replace ADfqlf=0 if Afqlf==0 & ADfqlf<0
replace ADfqlf=0 if havefq==0 & ADfqlf==.

**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "interq expq impq cdol cmil ctotCS extran1 extran2 avgprice avgpricef avgpricel"
foreach v in `varl' {
  gen AD`v'=N`v' -`v'
  gen AD`v'p=AD`v'*100/`v'
}
local varl "cdol cmil ctotCS"
foreach v in `varl' {
gen AD`v'pt=AD`v'/atcq
}

**change by county ******
local varl "cdol cmil ctotCS"
foreach v in `varl' {
gen AD`v'ptc=AD`v'/tcq
}

gen ADextran=ADcmilptc*wtcq*1.91/100

***benefits of reduction in export to break even with the hauling cost ***
*each ycounty has importq impq and cost ctotCS
by year: egen aADctotCS=total(ADctotCS*tcq) if year==2010  & dupc<=1 & havelocalc==1 
by year: egen aADimpq=total(ADimpq*tcq) if year==2010  & dupc<=1 & havelocalc==1 
gen hhvalue= - aADctotCS/aADimpq // $/ton of import for places reducing import
gen hhvaluec=-ADctotCS/ADimpq

*******************************************************************************
di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"

label var interq "CA intercounty waste (baseline)"
label var ADinterq "change in intercounty waste in CA"
label var expqp "percentage of export (baseline)"
label var expq "export amount (baseline)"
label var ADexpq "change in export amount"
label var impqp "percentage of import (baseline)"
label var impq "import amount (baseline)"
label var ADimpq "change in import amount"
label var cdolpt "tipping fees (baseline)"
label var ADcdolptc "change in tipping fees"
label var cmilpt "mileage (baseline)"
label var ADcmilptc "change in mileage"
label var ADctotCSptc "change in discarding cost"
label var extran1 "external transport cost (baseline)"
label var ADextran "change in external transport cost"
label var hhvalue "household value"
label var fqf "waste at intercounty sites"
label var ADfqf "change in waste at intercounty sites"
label var fql "waste at intracounty sites"
label var ADfql "change in waste at intracounty sites"
label var fqff "intercounty waste at intercounty sites"
label var ADfqff "change in intercounty waste at intercounty sites"
label var ADfqlf "change in intercounty waste at intracounty sites"
label var avgprice "average price at a facility (baseline)"
label var ADavgprice "change in average price at a facility"
label var ADavgpricep "change in average price at a facility (%)"
label var avgpricef "average price at non-local facilities (baseline)"
label var ADavgpricef "change in average price at non-local facilities"
label var ADavgpricefp "change in average price at non-local facilities (%)"
label var avgpricel "average price at local facilities (baseline)"
label var ADavgpricel "change in average price at local facilities"
label var ADavgpricelp "change in average price at local facilities (%)"

sum interq ADinterq expqp expq ADexpq impqp impq ADimpq cdolpt ADcdolptc ///
	cmilpt ADcmilptc ADctotCSptc extran1 ADextran hhvaluec ///
	[iw=tcq] if year==2010  & dupc<=1 & havelocalc==1 

sum avgprice ADavgprice ADavgpricep ///
	avgpricef ADavgpricef ADavgpricefp ///
	avgpricel ADavgpricel ADavgpricelp ///
	fqf ADfqf fql ADfql fqff ADfqff ADfqlf if year==2010 & tagf==1
	
 } // ending "`s'"!="importBan"
  
} // ending foreach s in `scenario'

